import { locations } from "../../../gpu/locations";
const ParticeGeometry =/*wgsl*/`
#include <Camera>
#include <Trans_def>
struct Particle {
  position : vec3<f32>,
  size : f32,
  color    : vec3<f32>,
  opacity:f32,
  velocity : vec3<f32>,
  age : f32,
  angle: f32,
  alive:f32,

}
@group(${locations.particle[0]}) @binding(${locations.particle[1]}) var<storage,read> particleLocalDatas: array<Particle>;
 
 struct Vertex {
    @builtin(vertex_index) VertexID:u32,
    @builtin(instance_index) instance_index : u32,
  };
  struct VertexOutput {
    @builtin(position) Position : vec4<f32>,
    @location(0) localPos:vec3<f32>,
    #if ~{def.map}
    @location(1) texcoord:vec2<f32>,
    #endif
    @location(2) color:vec4<f32>,
    #if ~{def.logarithmicDepth}
     @location(3) flogz : f32,
     #endif
  }
  @vertex
  fn main(vertexInput:Vertex) -> VertexOutput {

        var output:VertexOutput;
        var modelMatrix:mat4x4<f32>;
        var u_NormalMatrix:mat4x4<f32>;
        // 从trans中分解处模型矩阵与法向量矩阵
        #if ~{def.trans}
        modelMatrix=trans.modelMat;
        u_NormalMatrix=trans.normalMat;
        #endif

        let points = array(
          vec2f(-1, -1),
          vec2f( 1, -1),
          vec2f(-1,  1),
          vec2f(-1,  1),
          vec2f( 1, -1),
          vec2f( 1,  1),
        );
       let particle = particleLocalDatas[vertexInput.instance_index];
       if(particle.alive<1.0){
        output.color = vec4(0);
       }else{
        output.color = vec4(particle.color, particle.opacity);
       }
       let pos = points[vertexInput.VertexID];
      
        let c = cos(particle.angle);
        let s = sin(particle.angle);
        let rot = mat2x2f(
          c, s,
         -s, c,
       );
   
        #if ~{def.map}
         output.texcoord = pos * 0.5 + 0.5;
        #endif
        var position:vec4<f32> = vec4(particle.position, 1.0);
        var worldPosition= modelMatrix*position;
        var clipPos =camera.vp*worldPosition;
        let pointPos = vec4f(rot*pos * particle.size , 0, 0);
        output.Position = clipPos + pointPos;
        output.localPos=worldPosition.xyz/worldPosition.w;
        // 对数深度缓冲
       #if ~{def.logarithmicDepth}
        output.flogz = 1.0 + output.Position.w;
       #endif
        return output;
      }
`
export { ParticeGeometry }